<template>
  <div class="exam-detail">
    <div style="height: 40px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/testQuestion' }"
          >测试题库</el-breadcrumb-item
        >
        <el-breadcrumb-item>考试记录</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div
      class="test-table"
      v-loading="tableLoading"
      element-loading-text="拼命加载中"
    >
      <!-- 表格 -->
      <el-table :data="examDetailList" style="width: 100%; font-size: 17px">
        <el-table-column prop="testCategory" label="测试类型" align="center">
        </el-table-column>
        <el-table-column prop="testExamName" label="测试卷名" align="center">
        </el-table-column>
        <el-table-column prop="totalScore" label="所得总分" align="center">
        </el-table-column>
        <el-table-column prop="spentTime" label="所用时间" align="center">
        </el-table-column>
        <el-table-column label="完成时间" align="center">
          <template slot-scope="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="toDetail(scope.row.id)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-show="examDetailList.length > 0"
        layout="prev, pager, next"
        :current-page="currentPage"
        @current-change="handleCurrentChange"
        :total="total"
        :page-count="7"
        style="text-align: right; margin-top: 20px"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getExamDetail } from "@/api/client/examDetail";
export default {
  name: "ExamDetailView",
  data() {
    return {
      page: 1,
      pageSize: 10,
      total: 0,
      currentPage: 1,
      examDetailList: [],
      tableLoading: false,
    };
  },
  created() {
    this.getExamDetail();
  },
  methods: {
    // 获取用户考试记录
    async getExamDetail() {
      try {
        this.tableLoading = true;
        const res = await getExamDetail({
          page: this.page,
          pageSize: this.pageSize,
        });
        this.examDetailList = res.data.records;
        this.total = res.data.total;
      } finally {
        this.tableLoading = false;
      }
    },
    // 时间格式判断
    padZero(num) {
      return num < 10 ? "0" + num : num;
    },
    // 时间格式化
    formatDate(timeArray) {
      if (!timeArray) return "暂无时间";
      let year = timeArray[0];
      let month = this.padZero(timeArray[1]);
      let day = this.padZero(timeArray[2]);
      let hour = this.padZero(timeArray[3]);
      let minute = this.padZero(timeArray[4]);
      let second = this.padZero(timeArray[5]) || "00";
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
    // 分页查询
    handleCurrentChange(page) {
      this.page = page;
      this.currentPage = page;
      this.getExamDetail();
    },
    // 查看详情
    toDetail(id) {
      this.$router.push({
        name: "ExamDetail",
        params: { id: id },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.exam-detail {
  width: 100%;
  height: 100%;
  padding: 10px 60px;
  background-color: #fff;
  .test-table {
    width: 96%;
    height: 100%;
    margin: 0 auto;
    padding: 20px;
    box-shadow: 1px 1px 8px rgba(0, 0, 0, 0.15);
    transition: 0.2s;
    &:hover {
      box-shadow: 1px 1px 12px rgba(0, 0, 0, 0.2);
    }
  }
}
// 解决下拉框显示不完整问题
/deep/.el-select-dropdown__wrap.el-scrollbar__wrap {
  margin-bottom: 0 !important;
}
</style>